package com.business.action.oa;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.business.bean.oa.WorkApplyTime;
import com.business.bean.system.UserBean;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.sf.rose.jdbc.DBUtils;
import net.sf.rose.jdbc.PageBean;
import net.sf.rose.jdbc.dao.BeanDAO;
import net.sf.rose.jdbc.query.BeanSQL;
import net.sf.rose.jdbc.service.Service;
import net.sf.rose.web.util.WebUtils;

/**
 * 请假
 * 
 * @author fengjian
 *
 */
@Controller
@RequestMapping("/oa/apply")
public class ApplyAction {

	/**
	 * 
	 */
	@ResponseBody
	@RequestMapping("/page.do")
	public PageBean page(HttpServletRequest request, Service service) {
		PageBean page = WebUtils.getPageBean(request);
		Map<String, Object> map = WebUtils.getRequestData(request);
		BeanDAO dao = new BeanDAO(service);
		BeanSQL query = dao.getQuerySQL();
		query.setPage(page);
		query.setEntityClass(WorkApplyTime.class);
		query.setTableName("OA_WORK_APPLYTIME_VIEW");
		query.createSql(map);
		dao.list();
		return page;
	}

	/**
	 * 
	 */
	@ResponseBody
	@RequestMapping("/save.do")
	public PageBean save(Service service, WorkApplyTime bean) {
		PageBean res = new PageBean();
		BeanDAO dao = new BeanDAO(service);
		BeanSQL query = dao.getQuerySQL();
		query.setEntityClass(UserBean.class);
		query.createSql(bean.getUserID());
		UserBean user = dao.get();
		bean.setCheckUserID(user.getLeaderID());
		if (DBUtils.update(service, bean) > 0) {
			res.setMessage("保存信息成功！");
		} else {
			res.setSuccess(false);
			res.setMessage("保存信息失败！");
		}
		return res;
	}

	/**
	 * 删除
	 */
	@ResponseBody
	@RequestMapping("/delete.do")
	public PageBean delete(Service service, String supplyID) {
		return DBUtils.delete(service, WorkApplyTime.class, supplyID);
	}

	/**
	 * 删除
	 */
	@ResponseBody
	@RequestMapping("/check.do")
	public PageBean check(Service service, int status, String userID, String ids) {
		PageBean res = new PageBean();
		String[] arr = StringUtils.split(ids, '.');
		Date d = new Date();
		java.sql.Timestamp today = new java.sql.Timestamp(d.getTime());
		BeanDAO dao = new BeanDAO(service);
		BeanSQL query = dao.getQuerySQL();
		query.setEntityClass(WorkApplyTime.class);
		query.setSQL("select * from OA_WORK_APPLYTIME where applyTimeID=?");
		List<WorkApplyTime> list = new ArrayList<>();
		for (String id : arr) {
			query.addParameter(id);
			WorkApplyTime w = dao.get();
			w.setStatus(status);
			w.setCheckUserID(userID);
			w.setCheckTime(today);
			list.add(w);
			query.clearParameterList();
		}
		query.createBatchUpdateSql("status,checkTime,checkUserID", list);
		int[] is = dao.batchUpdate();
		if (dao.getBatchUpdateRecordCount() > 0) {
			// 如果是审批通过
			if (status == 1) {
				/* WorkDayReportService wrs = new WorkDayReportService(service);
				for (int i = 0; i < list.size(); i++) {
					if (is[i] > 0) {
						WorkApplyTime bean = list.get(i);
						wrs.userApplyAction(bean);
					}
				} */
			}
			// 如果是审批不通过
			else if (status == 2) {
				query.setSQL("delete from DEV_FACE_RECORD where applyID=?");
				for (int i = 0; i < list.size(); i++) {
					query.addParameter(list.get(i).getApplyTimeID());
					dao.update();
					query.clearParameterList();
				}
			}
			res.setMessage("保存信息成功！");
		} else {
			res.setSuccess(false);
			res.setMessage("保存信息失败！");
		}
		return res;
	}

/**
	   * @description 通过的请假列表
	   * @author  jiangwenfeng
	   * @date 创建时间：2019/10/19 23:10
	  */
	 @ResponseBody
	 @RequestMapping("/getWorkApplyRecordList.do")
	 public PageBean getWorkApplyRecordList(HttpServletRequest request, Service service) {
		 PageBean page = WebUtils.getPageBean(request);
		 Map<String, Object> map = WebUtils.getRequestData(request);
		 map.put("status",1);
		 BeanDAO dao = new BeanDAO(service);
		 BeanSQL query = dao.getQuerySQL();
		 query.setPage(page);
		 query.setEntityClass(WorkApplyTime.class);
		 query.setTableName("OA_WORK_APPLYTIME_VIEW");
		 query.createSql(map);
		 dao.list();
		 return page;
	 }
}
